package com.ma4567.remotecontroller.FileRequest;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ma4567.remotecontroller.MysqlHelper.TableLogManager;
import com.ma4567.remotecontroller.MysqlHelper.TableRequestLog;
import com.ma4567.remotecontroller.RemoteControllerApplication;
import com.ma4567.remotecontroller.ffmpegManager.ffmpegController;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;

class GetCurrentPathRespone{
    public int Status;//0 正常 1 cookie错误
    public String Path;

    public GetCurrentPathRespone(int Status,String Path){
        this.Status = Status;
        this.Path = Path;
        this.Path = this.Path.replace("\\", "%5C");
    }
}

@CrossOrigin
@RestController
public class GetCurrentPath {
    @RequestMapping(value = "/api/getCurrentPath", method = RequestMethod.GET)
    public String getCurrentPath(HttpServletRequest request,
                                 @RequestParam("cookie") String Cookie) throws SQLException {
        ffmpegController.StopStream();
        TableRequestLog.AddLog("Request current path", Cookie, request.getRemoteAddr());
        if(!RemoteControllerApplication.userManager.JudgeCookie(Cookie)){//鉴权
            return JSONObject.toJSONString(new GetCurrentPathRespone(1, "null"), SerializerFeature.PrettyFormat);
        }
        else {
            TableLogManager.AddLog("Request current path", Cookie);
            return JSONObject.toJSONString(new GetCurrentPathRespone(0, RemoteControllerApplication.userManager.getLoggedUserByCookie(Cookie).CurrentPath), SerializerFeature.PrettyFormat);
        }
    }
}